package com.atguigu.dga.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.atguigu.dga.governance.service.MainAssessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 治理考评结果明细 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/governance")
public class GovernanceAssessController {

   // List  list=new ArrayList();

    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;

    @Autowired
    MainAssessService mainAssessService;

    @GetMapping("/globalScore")
    public String getGlobalScore(){

        QueryWrapper<GovernanceAssessGlobal> queryWrapper = new QueryWrapper<GovernanceAssessGlobal>()
                .inSql("assess_date", "select max(assess_date) from  governance_assess_global");
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate",governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore",governanceAssessGlobal.getScore());

         List<BigDecimal> scoreList = new ArrayList<>();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());

        jsonObject.put("scoreList",scoreList);

        return jsonObject.toJSONString();
    }


    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public String getProblemList(@PathVariable("governType") String governType,
                                 @PathVariable("pageNo") int pageNo,
                                 @PathVariable("pageSize") int pageSize){
        int fromRowNo= (pageNo-1)*pageSize;
        QueryWrapper<GovernanceAssessDetail> queryWrapper =
                new QueryWrapper<GovernanceAssessDetail>()
                        .inSql("assess_date", "select max(assess_date) from  governance_assess_global")
                        .eq("governance_type",governType)
                        .lt("assess_score",BigDecimal.TEN)
                .last("limit " + fromRowNo + "," + pageSize);
        List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.list(queryWrapper);

         return  JSON.toJSONString(governanceAssessDetailList);


    }

    @GetMapping("/problemNum")
    public String getProblemNum(){
        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .select("governance_type", "count(*) as ct")
                .inSql("assess_date", "select max(assess_date) from  governance_assess_global")
                .lt("assess_score", BigDecimal.TEN)
                .groupBy("governance_type");


        //当前结构：   [{"governance_type":"SPEC","ct":10},{"governance_type":"CALC","ct":223} ... ]
        List<Map<String, Object>> mapList = governanceAssessDetailService.listMaps(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        for (Map<String, Object> map : mapList) {
            String governanceType =(String) map.get("governance_type");
            Long ct =(Long) map.get("ct");

            jsonObject.put(governanceType,ct);
        }

        return jsonObject.toJSONString();

    }

    @GetMapping("/rankList")
    public  String getRankList(){
        QueryWrapper<GovernanceAssessTecOwner> queryWrapper = new QueryWrapper<GovernanceAssessTecOwner>()
                .inSql("assess_date", "select max(assess_date) from  governance_assess_global")
                .select(" tec_owner as  tecOwner", "score");

        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(queryWrapper);
        return JSON.toJSONString(mapList);


    }

    @PostMapping("/assess/{date}")
    public String assess(@PathVariable("date") String assessDate){

        mainAssessService.mainAssess(assessDate);
        return "success";
    }
}
